/**
 * Record pre-delete trigger.
 * Validate the record to be deleted.
 * When false is returned the record will not be deleted in the database.
 * When an exception is thrown the record will also not be deleted in the database but it will be added to databaseManager.getFailedRecords(),
 * the thrown exception can be retrieved via record.exception.getValue().
 *
 * @param {JSRecord<db:/pigeon_board/adm_t900_perfiles>} record record that will be deleted
 *
 * @returns {Boolean}
 *
 * @private
 *
 * @properties={typeid:24,uuid:"93D4E423-4AA3-44B4-8543-830BE9BEDCC5"}
 */
function onRecordDelete(record) {

	// Borramos registros históricos
	var bok = false;
	bok = record.adm_t900_perfiles_to_adm_t300_perfiles_menus.deleteAllRecords();
	if (bok == false){
		application.output("No se ha podido borrar el histórico adm_t900_perfiles_to_adm_t300_perfiles_menus", LOGGINGLEVEL.DEBUG);
		return false;
	}
	bok = record.adm_t900_perfiles_to_adm_t900_roles.deleteAllRecords();
	if (bok == false){
		application.output("No se ha podido borrar el histórico adm_t900_perfiles_to_adm_t900_roles", LOGGINGLEVEL.DEBUG);
		return false;
	}
	bok = record.adm_t900_perfiles_to_adm_t900_usuarios.deleteAllRecords();
	if (bok == false){
		application.output("No se ha podido borrar el histórico adm_t900_perfiles_to_adm_t900_usuarios", LOGGINGLEVEL.DEBUG);
		return false;
	}
	
	return true;
}

/**
 * Record pre-insert trigger.
 * Validate the record to be inserted.
 * When false is returned the record will not be inserted in the database.
 * When an exception is thrown the record will also not be inserted in the database but it will be added to databaseManager.getFailedRecords(),
 * the thrown exception can be retrieved via record.exception.getValue().
 *
 * @param {JSRecord<db:/pigeon_board/adm_t900_perfiles>} record record that will be inserted
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"DB4B9C36-BAB5-465D-909C-82C1E7964CDD"}
 * @AllowToRunInFind
 */
function onRecordInsert(record) {
	
	// Recorremos el Listado de Menús perteneciente a la aplicación y los damos de alta
	/** @type {JSFoundSet<db:/pigeon_board/adm_t900_menus>} */
	var fs_adm_t900_menus = databaseManager.getFoundSet(scopes.adm_t900_menus.$DATA_SOURCE);
	fs_adm_t900_menus.loadAllRecords();
	if (fs_adm_t900_menus.find()){
		fs_adm_t900_menus.adm_t900_aplicaciones_id = record.adm_t900_aplicaciones_id;
		fs_adm_t900_menus.search();
	}
	
	/** @type {JSFoundSet<db:/pigeon_board/adm_t300_perfiles_menus>} */
	var fs_adm_t300_perfiles_menus = databaseManager.getFoundSet(scopes.adm_t300_perfiles_menus.$DATA_SOURCE);
	for (var x = 1; x <= databaseManager.getFoundSetCount(fs_adm_t900_menus); x++){
		fs_adm_t300_perfiles_menus.newRecord();
		fs_adm_t300_perfiles_menus.adm_t900_menus_id = fs_adm_t900_menus.getRecord(x).adm_t900_menus_id;
		fs_adm_t300_perfiles_menus.adm_t900_perfiles_id = record.adm_t900_perfiles_id;
	}
	return databaseManager.saveData(fs_adm_t300_perfiles_menus);
}
